// pages/AddressList/AddressList.js
const util = require('../../utils/util.js')
const requestManager = require('../../utils/request.js')
var router = require('../../utils/router.js')
const pageName = "CardsCenter "
var app = getApp();
let optionsEbuyId = 0
let optionsProductId = 0
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    isIpx: app.globalData.isIpx ? true : false, 
    editIndex:0,
    listLoading: false, // 是否正在加载数据
    addressList: [],
    isCellBack:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.ebuyId){
    optionsEbuyId = options.ebuyId
    optionsProductId = options.productId
    }
    this._getData()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 获取数据
   */
  _getData() {

    let params = {
      userId: util.getUserInfo().userId,
    }
    this.setData({
      listLoading: true,
      showLoading: true
    })
    requestManager.getDataResult(requestManager.address_list, params)
      .then(resolve => {
        // 状态还原

        if (resolve.list != undefined && resolve.list != null && resolve.list.length > 0) {
            // 已使用
            this.setData({
              listLoading:false,
              addressList: resolve.list,
              showLoading: false
            })
        } else {
          this.setData({
            listLoading: false,
            showLoading: false
          })
            util.noIconToast("您还没有地址信息")
        }
      }, reject => {

        this.setData({
          showLoading: false
        })

        // 状态还原
         this.data.listLoading = false

        if (reject.code == 2000) {
          util.noIconToast(reject.msgContent)
        }
      })
  },
  goOrder(e){
    if (optionsEbuyId==0){
      return false
    }
    let model = this.data.addressList[e.currentTarget.dataset.index]
    let params = ["addressId", model.addressId, "contacts", model.contacts, "phone", model.phone, "provinceId", model.provinceId, "cityId", model.cityId, "countyId", model.countyId, "address", model.address, "addressText", model.addressText, "isDefault", model.isDefault, "addressId", model.addressId, "ebuyId", `${optionsEbuyId}`, "productId", `${ optionsProductId}`]
    //地址pages/AddOrEditAddress/AddOrEditAddress
    let url = "/pages/orderconfirm/orderconfirm";
    router.route.setAction(router.REDIRECT_TO)
      .setParams(params)
      // .setReportName("AddOrEditAddress")
      .setUrl(url)
      .run();
  },
  selectedAddress:function(e){

    let dataset = e.currentTarget.dataset;
    let array = getCurrentPages();
    let page = array[array.length - 2];
    if (page.data.orderUserInfo == undefined) {
      return 
    }
    page.data.orderUserInfo.contacts = dataset.contacts
    page.data.orderUserInfo.phone = dataset.phone
    page.data.orderUserInfo.areaText = dataset.addressText
    page.data.orderUserInfo.provinceId = dataset.provinceId
    page.data.orderUserInfo.cityId = dataset.cityId
    page.data.orderUserInfo.countyId = dataset.countyId
    page.data.orderUserInfo.address = dataset.address
    page.data.orderUserInfo.addressId = dataset.addressId

    console.log(page.data.orderUserInfo)
    page.setData({
      orderUserInfo: page.data.orderUserInfo
    })
    wx.navigateBack({
      delta: 1,
    });

  },
  /**
  * 编辑地址
  */
  toAddressEdit: function (e) {
    let dataset = e.currentTarget.dataset;
    this.data.editIndex = dataset.index
    console.log(dataset.index)
    let model = this.data.addressList[dataset.index]

    let params = ["addressId", model.addressId, "contacts", model.contacts, "phone", model.phone, "provinceId", model.provinceId, "cityId", model.cityId, "countyId", model.countyId, "address", model.address, "addressText", model.addressText, "isDefault", model.isDefault, "addressId", model.addressId,

    ]
    //地址pages/AddOrEditAddress/AddOrEditAddress
    let url = "/pages/AddOrEditAddress/AddOrEditAddress";
    router.route.setAction(router.NAVIGATE_TO)
      .setParams(params)
      .setReportName("AddOrEditAddress")
      .setUrl(url)
      .run();

    util.reportAnalytics("to_address_edit");

  },
  /**
  * 新增地址
  */
  toAddAddressEdit: function (e) {
    
    this.data.editIndex = this.data.addressList.length;
    let params = []
    let url = "/pages/AddOrEditAddress/AddOrEditAddress";
    router.route.setAction(router.NAVIGATE_TO)
      .setParams(params)
      .setReportName("AddOrEditAddress")
      .setUrl(url)
      .run();

    util.reportAnalytics("to_address_edit");

    console.log("新增地址")
  },
  /**
  * 删除地址
  */
  deleteData: function (e) {
    let dataset = e.currentTarget.dataset;
    console.log(dataset.index)

    let model = this.data.addressList[dataset.index]
    this.data.listLoading = true
    this.setData({
      showLoading: true
    })
    let params = {
      userId: util.getUserInfo().userId,
      addressId: model.addressId
    }
    console.log("sdfasdfasdf",model)
    requestManager.getDataResult(requestManager.address_delete, params)
      .then(resolve => {
        // 状态还原
        this.data.listLoading = false
        this._getData()
        
      }, reject => {

        this.setData({
          showLoading: false
        })

        // 状态还原
        this.data.listLoading = false

        if (reject.code == 2000) {
          util.noIconToast(reject.msgContent)
        }
      })

   }

})